/**
 * C++ 控制流分析查询
 * 分析程序的控制流路径和执行顺序
 * 
 * 输出列：
 *  - function_name: 函数名
 *  - control_flow_type: 控制流类型
 *  - flow_location: 控制流位置
 *  - condition_expr: 条件表达式
 *  - branch_type: 分支类型
 */

import cpp
import semmle.code.cpp.controlflow.Guards

/**
 * 控制流分析：条件分支
 */
from IfStmt ifStmt, Expr condition
where
  ifStmt.getCondition() = condition
select
  ifStmt.getEnclosingFunction().getQualifiedName() as function_name,
  "ConditionalBranch" as control_flow_type,
  ifStmt.getLocation().toString() as flow_location,
  condition.toString() as condition_expr,
  "IfStatement" as branch_type

/**
 * 控制流分析：循环结构
 */
from ForStmt forStmt, Expr init, Expr condition, Expr update
where
  forStmt.getInit() = init and
  forStmt.getCondition() = condition and
  forStmt.getUpdate() = update
select
  forStmt.getEnclosingFunction().getQualifiedName() as function_name,
  "LoopControl" as control_flow_type,
  forStmt.getLocation().toString() as flow_location,
  condition.toString() as condition_expr,
  "ForLoop" as branch_type

from WhileStmt whileStmt, Expr condition
where
  whileStmt.getCondition() = condition
select
  whileStmt.getEnclosingFunction().getQualifiedName() as function_name,
  "LoopControl" as control_flow_type,
  whileStmt.getLocation().toString() as flow_location,
  condition.toString() as condition_expr,
  "WhileLoop" as branch_type

from DoStmt doStmt, Expr condition
where
  doStmt.getCondition() = condition
select
  doStmt.getEnclosingFunction().getQualifiedName() as function_name,
  "LoopControl" as control_flow_type,
  doStmt.getLocation().toString() as flow_location,
  condition.toString() as condition_expr,
  "DoWhileLoop" as branch_type

/**
 * 控制流分析：异常处理
 */
from TryStmt tryStmt
select
  tryStmt.getEnclosingFunction().getQualifiedName() as function_name,
  "ExceptionHandling" as control_flow_type,
  tryStmt.getLocation().toString() as flow_location,
  "try" as condition_expr,
  "TryBlock" as branch_type

from CatchStmt catchStmt, Variable exception
where
  catchStmt.getVariable() = exception
select
  catchStmt.getEnclosingFunction().getQualifiedName() as function_name,
  "ExceptionHandling" as control_flow_type,
  catchStmt.getLocation().toString() as flow_location,
  exception.getType().toString() as condition_expr,
  "CatchBlock" as branch_type

/**
 * 控制流分析：跳转语句
 */
from GotoStmt gotoStmt, Label label
where
  gotoStmt.getTarget() = label
select
  gotoStmt.getEnclosingFunction().getQualifiedName() as function_name,
  "JumpControl" as control_flow_type,
  gotoStmt.getLocation().toString() as flow_location,
  label.getName() as condition_expr,
  "GotoStatement" as branch_type

from BreakStmt breakStmt
select
  breakStmt.getEnclosingFunction().getQualifiedName() as function_name,
  "JumpControl" as control_flow_type,
  breakStmt.getLocation().toString() as flow_location,
  "break" as condition_expr,
  "BreakStatement" as branch_type

from ContinueStmt continueStmt
select
  continueStmt.getEnclosingFunction().getQualifiedName() as function_name,
  "JumpControl" as control_flow_type,
  continueStmt.getLocation().toString() as flow_location,
  "continue" as condition_expr,
  "ContinueStatement" as branch_type

from ReturnStmt returnStmt, Expr returnValue
where
  returnStmt.getValue() = returnValue
select
  returnStmt.getEnclosingFunction().getQualifiedName() as function_name,
  "JumpControl" as control_flow_type,
  returnStmt.getLocation().toString() as flow_location,
  returnValue.toString() as condition_expr,
  "ReturnStatement" as branch_type

/**
 * 控制流分析：函数调用路径
 */
from Function caller, Function callee, Call call
where
  call.getEnclosingFunction() = caller and
  call.getTarget() = callee
select
  caller.getQualifiedName() as function_name,
  "FunctionCall" as control_flow_type,
  call.getLocation().toString() as flow_location,
  callee.getQualifiedName() as condition_expr,
  "CallStatement" as branch_type
